package com.shchuangzhi.smartguard;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elvishew.xlog.XLog;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.InOutRecord;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by j_hu on 5/18/17.
 */

public class InOutRecordViewAdapter extends RecyclerView.Adapter {
    private List<InOutRecord> list;

    public InOutRecordViewAdapter(List<InOutRecord> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_inout, null);
        //不知道为什么在xml设置的“android:layout_width="match_parent"”无效了，需要在这里重新设置
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new InOutRecordViewAdapter.InOutRecordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        InOutRecordViewAdapter.InOutRecordViewHolder holder = (InOutRecordViewAdapter.InOutRecordViewHolder)viewHolder;
        InOutRecord rec = list.get(i);
        holder.tvName.setText(rec.getName());
        holder.tvState.setText(rec.getStateName());
        holder.tvNo.setText(new Integer(rec.getNo()).toString());
        holder.tvClass.setText(rec.getClassName());
        holder.imgHead.setImageResource(R.drawable.ic_person_black_60dp);
        holder.tvTime.setText(rec.getShortTimeStr());

        switch(rec.getState()) {
            case Consts.STATE_HEALTH: holder.tvState.setTextColor(Color.GREEN);
                break;
            case Consts.STATE_WATCH: holder.tvState.setTextColor(Color.BLUE);
                break;
            case Consts.STATE_SICK: holder.tvState.setTextColor(Color.RED);
                break;
            default:holder.tvState.setTextColor(Color.GREEN);
                break;
        }

        if (rec.getNo() / 1000 == 9) {
            holder.tvName.setTextColor(Color.rgb(255, 165, 0));
        } else {
            holder.tvName.setTextColor(Color.BLACK);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void reload(List<InOutRecord> list) {
        this.list = list;
    }

    class InOutRecordViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvTime;
        public TextView tvState;
        public TextView tvClass;
        public TextView tvNo;
        public ImageView imgHead;

        public InOutRecordViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView)itemView.findViewById(R.id.tv_list_inout_name);
            tvTime = (TextView)itemView.findViewById(R.id.tv_list_inout_time);
            tvState = (TextView)itemView.findViewById(R.id.tv_list_inout_state);
            tvNo = (TextView)itemView.findViewById(R.id.tv_list_inout_no);
            tvClass = (TextView)itemView.findViewById(R.id.tv_list_inout_class);
            imgHead = (ImageView)itemView.findViewById(R.id.img_list_inout_head);
        }
    }
}
